package part3;

import org.junit.Test;
import part3.c13_active_object.*;

/**
 * Created by lie on 2018/3/23.
 * @see #test2() 实现了多线程效果
 */
public class RTCMonoStateTest2 {

    @Test
    public void test() throws Exception {

        Command stopCommand = new Command() {
            public void execute() throws Exception {
                MultiDelayCommand.isStop = true;
                System.out.println("stopCommand executed");
            }
        };

        ActiveObjectEngine engine = new ActiveObjectEngine();
        MultiDelayCommand delayCommand1 = new MultiDelayCommand(engine, 'a');
        MultiDelayCommand delayCommand2 = new MultiDelayCommand(engine, 'b');
        MultiDelayCommand delayCommand3 = new MultiDelayCommand(engine, 'c');
        engine.addCommand(delayCommand1);
        engine.addCommand(delayCommand2);
        engine.addCommand(delayCommand3);

        SleepCommand sleepCommand1 = new SleepCommand(70, engine, stopCommand);
        SleepCommand sleepCommand2 = new SleepCommand(100, engine, stopCommand);
        SleepCommand sleepCommand3 = new SleepCommand(90, engine, stopCommand);
        engine.addCommand(sleepCommand1);
        engine.addCommand(sleepCommand2);
        engine.addCommand(sleepCommand3);


        engine.run();
    }


    @Test
    public void test2()throws Exception{

        MultiDelayCommand2 delayCommand1 = new MultiDelayCommand2(100, '1');
        MultiDelayCommand2 delayCommand2 = new MultiDelayCommand2(300, '3');
        MultiDelayCommand2 delayCommand3 = new MultiDelayCommand2(500, '5');
        MultiDelayCommand2 delayCommand4 = new MultiDelayCommand2(700, '7');

        MultiDelayCommand2.sEngine.addCommand(delayCommand1);
        MultiDelayCommand2.sEngine.addCommand(delayCommand2);
        MultiDelayCommand2.sEngine.addCommand(delayCommand3);
        MultiDelayCommand2.sEngine.addCommand(delayCommand4);

        Command stopCommand = new Command() {
            public void execute() throws Exception {
                MultiDelayCommand.isStop = true;
                System.out.println("stopCommand executed");
            }
        };
        //这个sleepCommand执行了总体的command的stop
        SleepCommand sleepCommand = new SleepCommand(20000, MultiDelayCommand2.sEngine, stopCommand);
        MultiDelayCommand2.sEngine.addCommand(sleepCommand);
        MultiDelayCommand2.sEngine.run();
    }
}
//两次执行结果有差异，（因为cpu时钟和实时时钟不可能完美同步，导致了行为不可确定）即可认为是多线程
//135711131511371113151131171351131115713111351113171135111311571311311511371113511311751311131517113111351137111513111357111311135171131511311713511311151371131115131713115113113571111311513171131513117113511131517131113151131711351113115713113115137111513111371151311135171131113511371151131113571131
//1357111311513711135111317113511131153711113151137115131113115731111351137111513111317151311131571131113511137115131113157113111351